/**
 * {地图key 申请}
 *
 * @author: 宋康
 * @date: 2023/1/10
 */
import VXETable from 'vxe-table'
import $axios from "@/network";
import {queryDictList} from "@/api/basics";

const resourceRoute = '/resource';

/**
 * {审核列表}
 *
 * @author: 宋康
 * @date: 2023/1/13
 */
export const resourceApplyPublic = {
  data() {
    return {
      showApproveDetailForm: false,
      durationDict: [],
      applyStatusDict: [],
      keyStatus: [],
      approveDetailData: {}
    }
  },
  methods: {
    /**
     * {获取字典}
     *
     * @author: 宋康
     * @date: 2023/1/13
     */
    queryDict() {
      return new Promise((resolve, reject) => {
        queryDictList(['APPLY_DURATION', 'APPLY_STATUS', 'KEY_STATUS']).then(res => {
          if (res.status) {
            const data = res.data;
            this.durationDict = data['APPLY_DURATION'];
            this.applyStatusDict = data['APPLY_STATUS'];
            this.keyStatus = data['KEY_STATUS'];
            resolve();
          } else {
            reject(res);
          }
        })
      })
    },
    /**
     * {格式化时长}
     *
     * @author: 宋康
     * @date: 2023/1/12
     */
    formatterDuration({cellValue}) {
      const duration = this.durationDict.filter(v => v['dictKey'] === cellValue);
      if (duration.length > 0) {
        return duration[0]['dictValue'];
      } else {
        return '未知'
      }
    },
    /**
     * {格式化状态}
     *
     * @author: 宋康
     * @date: 2023/1/12
     */
    formatterStatus({cellValue}) {
      const status = this.applyStatusDict.filter(v => v['dictKey'] === cellValue);
      if (status.length > 0) {
        return status[0]['dictValue'];
      } else {
        return '未知'
      }
    },
    /**
     * {格式化密钥状态}
     *
     * @author: 宋康
     * @date: 2023/1/29
     */
    formatterKeyStatus({cellValue}) {
      const status = this.keyStatus.filter(v => v['dictKey'] === cellValue['keyStatus']);
      if (status.length > 0) {
        return status[0]['dictValue'];
      } else {
        return '停止'
      }
    },
    /**
     * {下载附加文件}
     *
     * @author: 宋康
     * @date: 2023/1/12
     */
    downloadAdditFile(additFile) {
      downloadFile(additFile).then(res => {
        const contentDisposition = res.contentDisposition.split(";");
        let fileOp = {};
        for (let n of contentDisposition) {
          if (-1 !== n.indexOf("=")) {
            const f = n.split("=")
            fileOp[f[0]] = f[1];
          }
        }
        VXETable.saveFile({
          filename: decodeURI(fileOp['filename']),
          type: fileOp['filetype'],
          content: res
        })
      })
    },
    /**
     * {审批详情}
     *
     * @author: 宋康
     * @date: 2023/1/13
     */
    approveDetail(applicationRecordId) {
      queryApplyRecordDetailById(applicationRecordId).then(res => {
        if (res.status) {
          this.showApproveDetailForm = true;
          this.approveDetailData = res.data;
        }
      })
    },
  }
}
/**
 * {保存地图申请}
 *
 * @author: 宋康
 * @date: 2023/1/10
 */
export const saveMapApply = (options) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/apply/save`,
    // headers: {
    //   "Content-Type": "multipart/form-data"
    // },
    data: options
  })
}

/**
 * {获取用户地图申请列表}
 * @param current
 * @param size
 * @param op
 * @returns {*}
 * @author: 宋康
 * @date: 2023/1/11
 */
export const queryMyApplicationRecordIPage = (current, size, op) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/apply/query/application/page`,
    params: {
      current, size
    },
    data: op
  })
}

/**
 * {查询用户审批列表}
 * @param current
 * @param size
 * @param op
 * @returns {*|AxiosPromise}
 * @author: 宋康
 * @date: 2023/1/13
 */
export const queryApplicationApproveIPage = (current, size, op) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/apply/query/approve/page`,
    params: {
      current, size
    },
    data: {
      ...op
    }
  })
}
/**
 * {查询用户审批列表}
 * @param current
 * @param size
 * @param op
 * @returns {*|AxiosPromise}
 * @author: 宋康
 * @date: 2023/1/13
 */
export const queryApplicationResourceIPage = (current, size, op) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/apply/query/resource/page`,
    params: {
      current, size
    },
    data: {
      ...op
    }
  })
}
/**
 * {更新审批状态}
 * @param applicationRecordId
 * @param applyResult
 * @returns {*|AxiosPromise}
 * @author: 宋康
 * @date: 2023/1/14
 */
export const updateApproveResult = (applicationRecordId, applyResult) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/apply/approve`,
    data: {
      id,
      applyResult
    }
  })
}

/**
 * {查询指定申请单信息}
 * @param applicationRecordId
 * @returns {*|AxiosPromise}
 * @author: 宋康
 * @date: 2023/1/16
 */
export const queryApplyRecordDetailById = (applicationRecordId) => {
  return $axios({
    method: 'GET',
    url: `${resourceRoute}/apply/query/id`,
    params: {
      id: applicationRecordId
    }
  })
}

/**
 * {下载申请文件}
 * @param fileId
 * @author: 宋康
 * @date: 2023/1/18
 */
export const downloadFile = (fileId) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/file/download`,
    responseType: 'blob',
    params: {
      id: fileId
    }
  })
}

/**
 * {更新服务key状态}
 * @param applicationRecordId
 * @param statu
 * @returns {*|AxiosPromise}
 * @author: 宋康
 * @date: 2023/1/29
 */
export const updatedServiceStatus = (applicationRecordId, statu) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/apply/statu`,
    data: {
      applicationRecordId: applicationRecordId,
      statu: statu
    }
  })
}